package org.monitor.controller.applog;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.monitor.http.RestResult;
import org.monitor.kit.FileKit;
import org.monitor.model.AppMonitorManager;
import org.monitor.model.MonitorStateManager;
import org.monitor.servlet.controller.Controller;
import org.monitor.servlet.pairs.Pairs;
import org.monitor.servlet.pairs.PairsBuilder;

import com.google.gson.Gson;

/**
 * 日志
 * @author zhaojjd
 *
 */
public class AppLogController extends Controller {

	public void list(){
		getPairs().checkRequired("ip", "port", "monitor");
		
		String ip = getPair("ip");
		String port = getPair("port");
		String monitor = getPair("monitor");
		
		List<String> list = new ArrayList<String>();
		String storeDir = MonitorStateManager.getAppInstance().getMonitorDir(ip, port, monitor).toString();
		File dir = new File(storeDir);
		if(dir.exists()){
			File[] files = dir.listFiles();
			for (File file : files) {
				list.add(file.getName());
				
			}
			Collections.sort(list, new Comparator<String>(){
				@Override
				public int compare(String preName, String nextName) {
					return -1 * preName.compareTo(nextName);
				}
			});
		}
		
		boolean monitoring = MonitorStateManager.getAppInstance().hasMonitoring(ip, port, monitor);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("monitoring", monitoring);
		data.put("logs", list);
		
		RestResult rst = new RestResult();
		rst.setData(data);
		renderJson(rst);
	}
	
	public void get(){
		getPairs().checkRequired("ip", "port", "monitor", "name");
		
		String ip = getPair("ip");
		String port = getPair("port");
		String monitor = getPair("monitor");
		String name = getPair("name");
		
		String filename = MonitorStateManager.getAppInstance().getMonitorDir(ip, port, monitor).append(name).toString();
		File file = new File(filename);
		
		RestResult rst = new RestResult();
		
		String content = FileKit.read(file, "UTF-8");
		rst.setData(content);
		renderJson(rst);
	}
	
	public void del(){
		getPairs().checkRequired("ip", "port", "monitor", "name");
		
		String ip = getPair("ip");
		String port = getPair("port");
		String monitor = getPair("monitor");
		String name = getPair("name");
		
		String filename = MonitorStateManager.getAppInstance().getMonitorDir(ip, port, monitor).append(name).toString();
		File file = new File(filename);
		
		RestResult rst = new RestResult();
		
		boolean flag = FileKit.deleteAll(file);
		if(!flag){
			rst.setState(-1);
			rst.setMsg("删除失败！");
		}
		renderJson(rst);
	}

	public void record(){
		Pairs pairs = getAppReqPairs();
		
		pairs.checkRequired("ip", "port", "monitor");
		
		String ip = getPair(pairs, "ip");
		ip = ip.replace("http://", "");
		ip = ip.indexOf(":") != -1 ? ip.split(":")[0] : ip;
		
		String port = getPair(pairs, "port");
		port = port.replace("http://", "");
		port = port.indexOf(":") != -1 ? port.split(":")[1] : port;
		
		String monitor = getPair(pairs, "monitor");
		String content = getPair(pairs, "content");
		
		AppMonitorManager.getInstance(ip, port).log(monitor, content);
		RestResult rst = new RestResult();
		renderJson(rst);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private Pairs getAppReqPairs() {
		Pairs pairs = getPairs();
		byte[] bytes = getPairs().getPostData();
		if(bytes != null && bytes.length > 0){
			String postData = null;
			try {
				postData = new String(bytes, "UTF-8");
			} catch (Exception e) {
			}
			if(postData != null){
				Gson gson = new Gson();
				Map reqMap = gson.fromJson(postData, Map.class);
				List<Map> params = (List<Map>) ((Map)reqMap.get("reqparams")).get("Params");
				PairsBuilder builder = PairsBuilder.create(getPairs());
				for (Map map : params) {
					String name = (String) map.get("@id");
					String value = (String) map.get("value");
					builder.addPair(name, value);
				}
				pairs = builder.build();
			}
		}
		return pairs;
	}
}
